﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace WenSkin.Controls
{
    [Designer(typeof(Design.Designer.WenButtonDesigner))]
    public partial class WenButton : WenControl
    {
        public WenButton() : base()
        {
            InitializeComponent();
            this.Size = new Size(80, 30);
        }
        public WenButton(string text) : this()
        {
            Text = text;
        }

        #region 私有属性

        private Color backColor = Color.Transparent;
        private Image image = null;
        private Size imageSize = new Size(32, 32);

        #endregion

        #region 公有属性
        [Category("Wen"), DefaultValue(null)]
        public Image Image { get => image; set { image = value; this.Invalidate(); } }
        [Category("Wen")]
        [DefaultValue(typeof(Size), "32,32")]
        public Size ImageSize { get => imageSize; set { imageSize = value; this.Invalidate(); } }
        [Category("Wen")]
        [DefaultValue(TextImageRelation.ImageBeforeText)]
        public TextImageRelation TextImageRelation { get; set; } = TextImageRelation.ImageBeforeText;

        public override string Text { get => base.Text; set { base.Text = value; this.Invalidate(); } }

        [Category("Wen"), Description("边框样式"), DefaultValue(WenButtonBorderStyle.None)]
        public WenButtonBorderStyle BorderStyle { get; set; }
        #endregion

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;
            Rectangle rec = this.ClientRectangle;

            Brush brush = new SolidBrush(this.ForeColor);
            if (!this.Enabled)
                brush = new SolidBrush(Color.FromArgb(100, 100, 100));

            Rectangle recimage;
            Rectangle recString;
            if (TextImageRelation == TextImageRelation.ImageAboveText)
            {
                //图标绘制靠上居中
                recimage = new Rectangle((this.Width - ImageSize.Width) / 2, 3, ImageSize.Width, ImageSize.Height);
                recString = new Rectangle(0, 3 + ImageSize.Height, this.Width, this.Height - 3 - ImageSize.Height);
                if (Image != null)
                    g.DrawImage(Image, recimage);
                g.DrawString(this.Text, Font, brush, recString, ControlHelper.StringContersCenter);
            }
            else if (TextImageRelation == TextImageRelation.Overlay || Image == null)
            {
                //图标绘制靠上居中
                recimage = new Rectangle((this.Width - ImageSize.Width) / 2, (this.Height - ImageSize.Height) / 2, ImageSize.Width, ImageSize.Height);
                _ = new Rectangle(0, 3 + ImageSize.Height, this.Width, this.Height - 3 - ImageSize.Height);
                if (Image != null)
                    g.DrawImage(Image, recimage);
                g.DrawString(this.Text, Font, brush, rec, ControlHelper.StringContersCenter);
            }
            else
            {
                //其他都是图标坐标文字靠图标
                recimage = new Rectangle(2, (this.Height - ImageSize.Height) / 2, ImageSize.Width, ImageSize.Height);
                recString = new Rectangle(2 + ImageSize.Width + 2, 0, this.Width - ImageSize.Width - 6, this.Height);
                if (Image != null)
                    g.DrawImage(Image, recimage);
                g.DrawString(this.Text, Font, brush, recString, ControlHelper.StringConters);
            }


            //绘制图框
            using Pen pen = new Pen(Color.FromArgb(153, 153, 153));
            switch (BorderStyle)
            {
                case WenButtonBorderStyle.Rectangle:
                    g.DrawRectangle(pen, new Rectangle(0, 0, this.Width - 1, this.Height - 1));
                    break;
                case WenButtonBorderStyle.RoRectangle:
                    System.Drawing.Drawing2D.GraphicsPath path = ControlHelper.CreateRoundedRectanglePath(new Rectangle(0, 0, Width - 1, Height - 1), 10);
                    g.DrawPath(pen, path);
                    break;
                default:
                    break;
            }


        }
        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            this.BackColor = this.backColor;
        }
        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);
            this.backColor = this.BackColor;
            this.BackColor = Color.FromArgb(63, 63, 65);
        }
        protected override void OnClick(EventArgs e)
        {
            base.OnClick(e);
        }

        public enum WenButtonBorderStyle
        {
            None,
            Rectangle,
            RoRectangle,
        }
    }
}
